package cn.otrue.patienthealthmanager.adapter;

import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import cn.otrue.patienthealthmanager.R;
import cn.otrue.patienthealthmanager.bean.ThreadModel;
import cn.otrue.patienthealthmanager.utils.DensityUtil;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;

public class CommunityPostInfoAdpater extends BaseAdapter {

	private List<ThreadModel> good;
	private LayoutInflater inflater;
	private Context context;

	public void ImageSet(String imgurl, ImageView img_headPhoto) {

		DisplayImageOptions options = new DisplayImageOptions.Builder()
				.showImageOnLoading(R.drawable.iv_user_photo)
				.showImageOnFail(R.drawable.iv_user_photo)
				.showImageForEmptyUri(R.drawable.iv_user_photo)
				.cacheInMemory(true)
				.cacheOnDisk(true)
				.bitmapConfig(Bitmap.Config.RGB_565)
				.displayer(
						new RoundedBitmapDisplayer(DensityUtil.dip2px(context,
								40))).build();
		ImageLoader.getInstance().displayImage(imgurl, img_headPhoto, options);
	}

	public CommunityPostInfoAdpater(List<ThreadModel> good, Context context) {
		this.good = good;
		inflater = LayoutInflater.from(context);
		this.context = context;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return good.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return good.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		convertView = inflater
				.inflate(R.layout.copyofwode_partition_item, null);
		ImageView pe_head_image = (ImageView) convertView
				.findViewById(R.id.pe_head_image);

		TextView tv_community_name = (TextView) convertView
				.findViewById(R.id.tv_community_name);
		TextView tv_community_time = (TextView) convertView
				.findViewById(R.id.tv_community_time);
		TextView tv_community_num = (TextView) convertView
				.findViewById(R.id.tv_community_num);
		ThreadModel eachMap = good.get(position);

		tv_community_name.setText(eachMap.getReplyContent() + "");

		// long num = Long.parseLong(eachMap.getCreatetime().substring(0, 10)) ;
		// Date uuu = new Date(num);
		//
		// String dateStr = CommonUtil.getFormatDate("yyyy-MM-dd", uuu);
		tv_community_time.setText(eachMap.getCreatetime().substring(0, 10));
		tv_community_num.setText(eachMap.getNickName() + "");
		String url = eachMap.getHeadimg();
		ImageSet(url, pe_head_image);
		return convertView;
	}
}
